﻿using Root.Models.BLL;
using Root.Properties;
using SQLHelper;
using System.Data;
using System.Text;

namespace Root.Models.DAL
{
    public class CommonDAL
    {
        public static string connStr = Resources.SQLite_Root;
        public static string GetLocationByIPAddress(string ip)
        {
            DataTable dt = new SQLiteHelper(connStr).ExecuteDataTable("SELECT Location FROM IP where @IP between StartIPNum and EndIPNum", new SQLParameters.SQLite().New("@IP", CommonHelper.INET.INET_ATON(ip)));
            if (dt.Rows.Count <= 0)
            {
                return "未找到此IP对应的地址";
            }
            else if (dt.Rows.Count >= 2)
            {
                StringBuilder address = new StringBuilder();
                foreach (DataRow row in dt.Rows)
                {
                    address.AppendLine(row["Location"].ToString());
                }
                return address.ToString();
            }
            else
            {
                DataRow row = dt.Rows[0];
                string address = row["Location"].ToString();
                return address;
            }
        }
    }
}